<template>
  <div class="o-radio">
    <RadioGroup v-model="value" :vertical="isVertical" @on-change="onChange">
      <Radio
        v-for="(item, idx) of renderOpts.props.options"
        :label="$srv.getI18nByPath('i18n.' + item.label)"
        :disabled="renderOpts.props.disabled"
        :key="idx"
      ></Radio>
    </RadioGroup>
  </div>
</template>

<script lang="ts">
import { Tsrv, T_srv, ifs, ts, vue, I_cmd, T_cmd, N_oRadio } from './config';

@T_srv.comp
export default class oRadio extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  @T_srv.prop() renderOpts: any;

  @T_srv.prop() params: any;

  /* 组件名 */
  private name: string = 'oRadio';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  /* 指挥部 */
  private GT_command: I_cmd = new T_cmd();

  public isVertical: boolean = false;

  public value: string;

  created() {
    this.value = Tsrv.utils.noValue(this.renderOpts.props.options)
      ? null
      : Tsrv.getI18nByPath(`i18n.${this.renderOpts.props.options[0].label}`);
    this.setParentVal(this.renderOpts.props.options[0].label);
    this.isVertical =
      Tsrv.getx(this.renderOpts.props, 'radioArrang', 'h') === 'v';
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.GT_command.init(this, this.GT_compBoxInfo);
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
  }

  public onChange(e) {
    const opts: Array<N_oRadio.I_renderOptsPropsOptions> = this.renderOpts.props
      .options;
    for (const o of opts) {
      if (Tsrv.getI18nByPath(`i18n.${o.label}`) === e) {
        this.setParentVal(o.value);
        break;
      }
    }
    this.emitEvent('change');
  }

  public setValue(val: string) {
    const opts: Array<N_oRadio.I_renderOptsPropsOptions> = this.renderOpts
      .props;
    for (const o of opts) {
      if (o.value === val) {
        this.setParentVal(val);
        this.value = Tsrv.getI18nByPath(`i18n.${o.label}`);
        break;
      }
    }

    this.setParentVal(val);
  }

  private setParentVal(val: any) {
    if (this.params.property) {
      this.params.data[this.params.property] = val === '' ? null : val;
    } else {
      this.params.row[this.params.column.property] = val === '' ? null : val;
    }
  }

  public getValue(): string {
    for (const o of this.renderOpts.props.options) {
      if (Tsrv.getI18nByPath(`i18n.${o.label}`) === this.value) {
        return o.label;
      }
    }
    return '';
  }

  private emitEvent(event: string) {
    const ev = this.renderOpts.events;
    const params = Tsrv.utils.cloneNoMatchVue(this.params, true);
    params['from'] = 'inputtext';
    for (const action in ev) {
      if (ev[action] && action === `on-${event}`) {
        ev[action](params);
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
